package com.dunke.core.common;

import com.dunke.core.enums.ReturnCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应对象
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {
    private int code;
    private String message;
    private boolean success;
    private T data;

    public Result(int code, String message, boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }

    public Result(ReturnCode code) {
        this.code = code.getCode();
        this.message = code.getMessage();
        this.success = code.isSuccess();
    }

    /**
     * 访问成功，无返回数据
     * @return
     */
    public static Result success() {
        return new Result(ReturnCode.SUCCESS);
    }

    /**
     * 访问成功
     * @param message 返回信息
     * @return
     */
    public static Result success(String message) {
        Result result =  new Result(ReturnCode.SUCCESS);
        result.setMessage(message);
        return result;
    }

    /**
     * 访问成功
     * @param o 返回数据
     * @return
     */
    public static Result success(Object o) {
        Result result =  new Result(ReturnCode.SUCCESS);
        result.setData(o);
        return result;
    }

    /**
     * 访问失败
     * @param code 提示
     * @return
     */
    public static Result error(ReturnCode code) {
        return new Result(code);
    }

    /**
     * 访问失败
     * @param message 提示
     * @return
     */
    public static Result error(String message) {
        Result result =  new Result(ReturnCode.ERROR);
        result.setMessage(message);
        return result;
    }

    /**
     * 访问失败
     * @param message 提示
     * @param t 对象
     * @return
     */
    public static Result error(String message, Object t) {
        Result result =  new Result(ReturnCode.ERROR);
        result.setMessage(message);
        result.setData(t);
        return result;
    }

    /**
     * 没有权限
     * @param message
     * @return
     */
    public static Result notPermission(String message) {
        Result result =  new Result(ReturnCode.NOT_PERMISSION);
        result.setMessage(message);
        return result;
    }


    /**
     * 参数错误
     * @return
     */
    public static Result paramError() {
        return  new Result(ReturnCode.UN_VAILD_PARAM);
    }

    @Override
    public String toString() {
        return "Response{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", success=" + success +
                ", data=" + data +
                '}';
    }
}